import numpy as np
import matplotlib.pyplot as plt

def feature_extract():
    data = np.load('./predict_result/thruster_outlier_data.npy')
    extract_high_features_array = np.average(
        data.reshape(-1, 5, data.shape[2])[::2, :, :].reshape(data.shape[0], 100, data.shape[2]), axis=1)
    extract_low_features_array = np.average(
        data.reshape(-1, 5, data.shape[2])[1::2, :, :].reshape(data.shape[0], 100, data.shape[2]), axis=1)

    print(extract_high_features_array.shape)

    for i in range(data.shape[2]):
        plt.figure(figsize=(20, 16), dpi=80)
        plt.tick_params(labelsize=40)
        x = np.arange(extract_high_features_array.shape[0])
        ax = plt.subplot(111)
        plt.scatter(x, extract_high_features_array[:, i], s=1000)
        plt.plot(extract_high_features_array[:, i], linewidth=10)
        plt.title(u'提取特征', fontproperties='Microsoft YaHei', fontsize=40)
        plt.xlabel(u'产品编号', fontproperties='Microsoft YaHei', fontsize=40)
        plt.ylabel(u'属性值', fontproperties='Microsoft YaHei', fontsize=40)
        ax.yaxis.get_offset_text().set_fontsize(40)
        plt.tight_layout()
        plt.savefig("./img/high_feature" + str(i + 1) + ".png")
        # plt.show()
        plt.close()

        plt.figure(figsize=(20, 16), dpi=80)
        plt.tick_params(labelsize=40)
        x = np.arange(extract_low_features_array.shape[0])
        ax = plt.subplot(111)
        plt.scatter(x, extract_low_features_array[:, i], s=1000)
        plt.plot(extract_low_features_array[:, i], linewidth=10)
        plt.title(u'提取特征', fontproperties='Microsoft YaHei', fontsize=40)
        plt.xlabel(u'产品编号', fontproperties='Microsoft YaHei', fontsize=40)
        plt.ylabel(u'属性值', fontproperties='Microsoft YaHei', fontsize=40)
        ax.yaxis.get_offset_text().set_fontsize(40)
        plt.tight_layout()
        plt.savefig("./img/low_feature" + str(i + 1) + ".png")
        # plt.show()
        plt.close()
    np.save('./predict_result/feature_array.npy',np.array([extract_high_features_array, extract_low_features_array]))
    return [extract_high_features_array, extract_low_features_array]

if __name__ == "__main__":
    feature_extract()